package com.scm.erp.framework.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component(value = "failureHandler")
public class FailureHandler extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        Map<String,Object> msgMap = new HashMap<>();
        msgMap.put("code",0);
        msgMap.put("success",false);
        msgMap.put("error","账号错误或密码");
        String jsonObject =JSONObject.toJSONString(msgMap);
        System.out.println(jsonObject);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonObject);
    }
}
